package net.chrisrichardson.ec2deployer.core;

import net.chrisrichardson.ec2deployer.util.FileBasedTemplate
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory;

public abstract class Application {

  Log logger = LogFactory.getLog(getClass())

  long id
  EC2Server host

  protected Application() {

  }
  
  Application(host)  {
	  this.host = host
  }
  
  public String getHostPrivateDnsName() {
      return host.privateDnsName
  }

  String getHostPublicDnsName() {
      return host.publicDnsName
  }

    def writeFile(Map _args) {
        def fileName = _args["fileName"]
        def templateName = _args["templateName"]
        def args = _args["templateArgs"]
        def contents = _args["contents"]

        if (contents) {
            host.ssh("cat > $fileName", contents)
        }  else {
            FileBasedTemplate template = new FileBasedTemplate()
            def text = template.process(templateName, args)
            host.ssh("cat > $fileName", text)
        }
    }
    def appendToFile(Map _args) {
        def fileName = _args["fileName"]
        def text = _args["text"]
        host.ssh("cat >> $fileName", text);
    }

    protected void startService(String serviceName) {
        host.ssh("/sbin/service $serviceName start")
    }

    protected void restartService(String serviceName) {
        host.ssh("/sbin/service $serviceName restart")
    }

    protected void stopService(serviceName) {
        ignoringFailure("stop failed - ignoring") {
            host.ssh("/sbin/service $serviceName stop")
        }
	}

    def waitForHttp(Map _args) {
        def port = _args["port"]
        def path = _args["path"]
        def scheme = _args["scheme"]
        def tries = _args["tries"]
        def retryDelay = _args["retryDelay"]
        if (!tries) tries = 10
        if (!retryDelay) retryDelay = 10
        if (!scheme) scheme = "http"
        if (!port) {
            if (scheme == "https")
                port = 443
            else
                port = 80
        }
        def url = "$scheme://localhost:$port/$path"
       try {
            host.ssh("wget ${scheme == "https" ? "--no-check-certificate " : ""}--spider --tries=${tries} --retry-connrefused --waitretry=${retryDelay} $url")
        } catch (ProcessExecutionException e) {
            logger.warn("pinging $url failed", e)
        }
	}                 
	
    def exec(command, String text = null) {
        if (text)
        host.ssh(command, text)
        else
        host.ssh(command)
    }

    def exec(Map _args) {
        def command = _args["command"]
        def templateName = _args["templateName"]
        def args = _args["templateArgs"]

        FileBasedTemplate template = new FileBasedTemplate()
        def text = template.process(templateName, args)

        exec command, text
    }

  def startSar() {
      host.startSar()
  }

  def stopSar() {
      host.stopSar()
  }

  def getSarMonitor() {
      host.sarMonitor
  }

   Object ignoringFailure(String message, Closure closure) {
        try {
            closure.call()
        } catch (ProcessExecutionException  e) {
            logger.debug(message + " - ignoring", e)
        }
    }

}